package com.liyl.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author liyl
 * @date 2020/11/17 14:57
 **/
@Configuration
@EnableAsync
public class ExecutorConfig {

    @Autowired
    private Environment environment;

    /**
     * 异步线程池配置信息
     * @return
     */
    @Bean("taskExecutor")
    public AsyncTaskExecutor getAsyncTaskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(Integer.parseInt(environment.getProperty("thread.pool.maxPoolSize")));
        taskExecutor.setCorePoolSize(Integer.parseInt(environment.getProperty("thread.pool.corePoolSize")));
        taskExecutor.setKeepAliveSeconds(Integer.parseInt(environment.getProperty("thread.pool.keepAliveSeconds")));
        taskExecutor.setQueueCapacity(Integer.parseInt(environment.getProperty("thread.pool.queueCapacity")));
        taskExecutor.setThreadNamePrefix(environment.getProperty("thread.pool.threadNamePrefix"));
        taskExecutor.initialize();
        return taskExecutor;
    }
}
